﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using SportsStore.Domain.Entities;
using SportsStore.Domain.Abstract;
using SportsStore.WebUI.Controllers;
using System.Web.Mvc;
using SportsStore.WebUI.Models;
using SportsStore.WebUI.HtmlHelpers;


namespace SportsStore.Test
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class ProductTest
    {
        [TestMethod]
        public void Can_Paginate()
        {
            // Arrange 
            // - create the mock repository 
            Mock<IProductRepository> mock = new Mock<IProductRepository>();
            mock.Setup(m => m.Products).Returns(new Product[] { 
                new Product {ProductID = 1, Name = "P1"}, 
                new Product {ProductID = 2, Name = "P2"}, 
                new Product {ProductID = 3, Name = "P3"}, 
                new Product {ProductID = 4, Name = "P4"}, 
                new Product {ProductID = 5, Name = "P5"} 
            }.AsQueryable());
            // create a controller and make the page size 3 items 
            ProductController controller = new ProductController(mock.Object);
            controller.PageSize = 3;
            // Action 
            ProductsListViewModel result = (ProductsListViewModel)controller.List(null,2).Model;
            
            // Assert 
            Product[] prodArray = result.Products.ToArray();
            Assert.IsTrue(prodArray.Length == 2);
            Assert.AreEqual(prodArray[0].Name, "P4");
            Assert.AreEqual(prodArray[1].Name, "P5");
        }

        [TestMethod]
        public void Can_Generate_Page_Links() 
        {

            // Arrange - define an HTML helper - we need to do this 
            // in order to apply the extension method 
            HtmlHelper myHelper = null;

            // Arrange - create PagingInfo data 
            PagingInfo pagingInfo = new PagingInfo
            {
                CurrentPage = 2,
                TotalItems = 28,
                ItemsPerPage = 10
            };

            // Arrange - set up the delegate using a lambda expression 
            Func<int, string> pageUrlDelegate = i => "Page" + i;

            // Act 
            MvcHtmlString result = myHelper.PageLinks(pagingInfo, pageUrlDelegate);

            // Assert 
            Assert.AreEqual(result.ToString(), @"<a href=""Page1"">1</a><a class=""selected"" href=""Page2"">2</a><a href=""Page3"">3</a>");
        }

        [TestMethod]
        public void Can_Send_Pagination_ViewModel()
        {
            // Arrange 
            // - create the mock repository 
            Mock<IProductRepository> mock = new Mock<IProductRepository>();
            mock.Setup(m => m.Products).Returns(new Product[] { 
                new Product {ProductID = 1, Name = "P1"}, 
                new Product {ProductID = 2, Name = "P2"}, 
                new Product {ProductID = 3, Name = "P3"}, 
                new Product {ProductID = 4, Name = "P4"}, 
                new Product {ProductID = 5, Name = "P5"} 
            }.AsQueryable());
            // create a controller and make the page size 3 items 
            ProductController controller = new ProductController(mock.Object);
            controller.PageSize = 3;
            // Action 
            ProductsListViewModel result = (ProductsListViewModel)controller.List(null, 2).Model;
            
            // Assert 
            PagingInfo pageData = result.PageData; 
                                   
            Assert.AreEqual(pageData.CurrentPage, 2);
            Assert.AreEqual(pageData.ItemsPerPage , 3);
            Assert.AreEqual(pageData.TotalItems, 5);
            Assert.AreEqual(pageData.TotalPages, 2);
        }

        [TestMethod]
        public void Can_Filer_Products()
        {
            // Arrange 
            // - create the mock repository 
            Mock<IProductRepository> mock = new Mock<IProductRepository>();
            mock.Setup(m => m.Products).Returns(new Product[] { 
                new Product {ProductID = 1, Name = "P1", Category = "Cat1"}, 
                new Product {ProductID = 2, Name = "P2", Category = "Cat2"}, 
                new Product {ProductID = 3, Name = "P3", Category = "Cat1"}, 
                new Product {ProductID = 4, Name = "P4", Category = "Cat2"}, 
                new Product {ProductID = 5, Name = "P5", Category = "Cat3"} 
            }.AsQueryable());

            // Arrange - create a controller and make the page size 3 items 
            ProductController controller = new ProductController(mock.Object);
            controller.PageSize = 3;
            // Action 
            Product[] result = ((ProductsListViewModel)controller.List("Cat2", 1).Model)
                .Products.ToArray(); 

            //Assert
            Assert.AreEqual(result.Length, 2);
            Assert.IsTrue(result[0].Name == "P2" && result[0].Category == "Cat2");
            Assert.IsTrue(result[1].Name == "P4" && result[0].Category == "Cat2");
        }

        [TestMethod]
        public void Can_Create_Categories()
        {
            // Arrange 
            // - create the mock repository 
            Mock<IProductRepository> mock = new Mock<IProductRepository>();
            mock.Setup(m => m.Products).Returns(new Product[] { 
                new Product {ProductID = 1, Name = "P1", Category = "Apples"}, 
                new Product {ProductID = 2, Name = "P2", Category = "Apples"}, 
                new Product {ProductID = 3, Name = "P3", Category = "Plums"}, 
                new Product {ProductID = 4, Name = "P4", Category = "Oranges"}, 
                new Product {ProductID = 5, Name = "P5", Category = "Apples"} 
            }.AsQueryable());

            // Arrange - create a controller and make the page size 3 items 
            NavController controller = new NavController(mock.Object);
            
            // Action get result
            string[] result = ((IEnumerable<string>)controller.Menu().Model).ToArray();

            //Assert
            Assert.AreEqual(result.Length, 3);
            Assert.AreEqual(result[0],"Apples" );
            Assert.AreEqual(result[1], "Oranges");
            Assert.AreEqual(result[2], "Plums");
        }

        [TestMethod]
        public void Indicate_Selected_Category()
        {
            // Arrange 
            // - create the mock repository 
            Mock<IProductRepository> mock = new Mock<IProductRepository>();
            mock.Setup(m => m.Products).Returns(new Product[] { 
                new Product {ProductID = 1, Name = "P1", Category = "Apples"}, 
                new Product {ProductID = 2, Name = "P2", Category = "Apples"}, 
                new Product {ProductID = 3, Name = "P3", Category = "Plums"}, 
                new Product {ProductID = 4, Name = "P4", Category = "Oranges"}, 
                new Product {ProductID = 5, Name = "P5", Category = "Apples"} 
            }.AsQueryable());

            // Arrange - create a controller and make the page size 3 items 
            NavController controller = new NavController(mock.Object);

            //Selected Category
            string categorySelected = "Apples";

            // Action get result
            string result = controller.Menu(categorySelected).ViewBag.SelectedCategory;

            //Assert
            Assert.AreEqual(categorySelected, result);            
        }

        [TestMethod]
        public void Generate_Category_Specific_Product_Count()
        {
            // Arrange 
            // - create the mock repository 
            Mock<IProductRepository> mock = new Mock<IProductRepository>();
            mock.Setup(m => m.Products).Returns(new Product[] { 
                new Product {ProductID = 1, Name = "P1", Category = "Cat1"}, 
                new Product {ProductID = 2, Name = "P2", Category = "Cat2"}, 
                new Product {ProductID = 3, Name = "P3", Category = "Cat1"}, 
                new Product {ProductID = 4, Name = "P4", Category = "Cat2"}, 
                new Product {ProductID = 5, Name = "P5", Category = "Cat3"} 
            }.AsQueryable());

            // Arrange - create a controller and make the page size 3 items 
            ProductController controller = new ProductController(mock.Object);
            controller.PageSize = 3;
            // Action 
            ProductsListViewModel result = (ProductsListViewModel)controller.List("Cat1").Model;

            // Assert 
            PagingInfo pageData = result.PageData;

            Assert.AreEqual(pageData.CurrentPage, 1);
            Assert.AreEqual(pageData.ItemsPerPage, 3);
            Assert.AreEqual(pageData.TotalItems, 2);
            Assert.AreEqual(pageData.TotalPages, 1);
        }


        [TestMethod]
        public void Can_Retrieve_Image_Data()
        {

            // Arrange - create a Product with image data 
            Product prod = new Product
            {
                ProductID = 2,
                Name = "Test",
                ImageData = new byte[] { },
                ImageMimeType = "image/png"
            };

            // Arrange - create the mock repository 
            Mock<IProductRepository> mock = new Mock<IProductRepository>();
            mock.Setup(m => m.Products).Returns(new Product[] { 
        new Product {ProductID = 1, Name = "P1"}, 
        prod,                             
        new Product {ProductID = 3, Name = "P3"} 
    }.AsQueryable());

            // Arrange - create the controller 
            ProductController target = new ProductController(mock.Object);

            // Act - call the GetImage action method 
            ActionResult result = target.GetImage(2);

            // Assert 
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(FileResult));
            Assert.AreEqual(prod.ImageMimeType, ((FileResult)result).ContentType); 
        }
    }
}
